package com.koron.operation.util;

import com.koron.operation.dispatch.bean.EquipmentBean;
import com.koron.operation.dispatch.enums.OverhaulStatusEnum;
import com.koron.operation.startlog.enums.StartLogEnum;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IntactRatioUntil {

    /**
     * 计算标准台时
     * @param startDate
     * @param daysOfMonth
     * @param unitlist
     * @return
     */
    public static double queryStandTimes(Date startDate, int daysOfMonth, List<EquipmentBean> unitlist) {
        Integer countUnits = 0;
        for (EquipmentBean d : unitlist) {
            if ("run".equals(d.getRunningState()) && d.getStartUse() != null && d.getStartUse().getTime() <= startDate.getTime()) {
                countUnits++;
            }
        }
        // 运行状态机组数 * 单月天数 * 单日小时数
        Integer count = countUnits * daysOfMonth * 24;
        return (double) Math.round(count * 100) / 100;
    }

    /**
     * 判断总检修台时、非故障检修台时
     * @param status
     * @param type
     * @return
     */
    public static boolean isDisableUnitRecord(int status, String type) {
        if ("ALL".equals(type)) {
            return isDisableAllProd(status);
        } else {
            return isDisableNotProd(status);
        }
    }

    // 总检修台时：数据來至各单位检修工单（检修工作总时间）
    private static boolean isDisableAllProd(int status) {
        return OverhaulStatusEnum.OVERHAUL.getValue() == status ||
                OverhaulStatusEnum.TROUBLESHOOTING.getValue() == status ||
                OverhaulStatusEnum.planned.getValue() == status ||
                OverhaulStatusEnum.normal.getValue() == status;
    }

    // （非故障检修工作总时间）括大修、计划检修、正常维护
    private static boolean isDisableNotProd(int status) {
        return OverhaulStatusEnum.OVERHAUL.getValue() == status ||
                OverhaulStatusEnum.planned.getValue() == status ||
                OverhaulStatusEnum.normal.getValue() == status;
    }


    /**
     * 转换百分比
     */
    public static String parsePercent(Double d) {
        java.text.NumberFormat percentFormat = java.text.NumberFormat.getPercentInstance();
        percentFormat.setMaximumFractionDigits(2); //最大小数位数
        percentFormat.setMaximumIntegerDigits(3);//最大整数位数
        percentFormat.setMinimumFractionDigits(2); //最小小数位数
        percentFormat.setMinimumIntegerDigits(1);//最小整数位数
        return percentFormat.format(d);//自动转换成百分比显示
    }

    // 毫秒转小时
    public static double longtimeToHour(long time) {
        time = time / 1000 / 60; // 分钟数
        double hour = time / 60.0;
        BigDecimal bigDecimal = new BigDecimal(hour);
        // 四舍五入保留两位小数
        return bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    /**
     * 状态是否停机
     * @param type
     * @return
     */
    private static boolean isDownUnit(String  type) {
        return StartLogEnum.SHUT_DOWN.getStr() == type ||
                StartLogEnum.ACCIDENT.getStr() == type ||
                StartLogEnum.BREAKDOWN.getStr() == type;
    }


    /**
     * 转换简称
     * @param phoneString
     * @return
     */
    public static String getSimpleName(String phoneString){
        if(phoneString.equals("合计")){
            return "合计";
        }
        Pattern pattern = Pattern.compile("[^0-9]");
        Matcher matcher = pattern.matcher(phoneString);
        //将与模式匹配的输入序列的每个子序列替换为给定的替换字符串。
        String all = matcher.replaceAll("");
        String number = Pattern.compile("[^0-9]").matcher(phoneString).replaceAll("");
        all = all + "#机";
        return all;
    }

}

